package org.i0o.appliancemanager.ui.win;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import org.i0o.appliancemanager.ui.helper.UITools;
import org.i0o.appliancemanager.ui.win.BaseWin;
import org.i0o.appliancemanager.ui.win.MainWin;
import org.i0o.appliancemanager.ui.win.PosWin;

public class LoginWin extends BaseWin {

	private static final long serialVersionUID = 8362891410174511230L;

	private final int DEFAULT_WIDTH = 360;
	private final int DEFAULT_HEIGHT = 240;

	private JTextField txtUserName;
	private JPasswordField txtPassword;

	private JButton btnLogin;
	private JButton btnCancel;

	private JLabel lblTopImage;

	@Override
	protected void initLayout() {
		this.setLayout(new BorderLayout());

		this.lblTopImage = UITools.createLabel("");
		this.lblTopImage.setOpaque(true);
		this.lblTopImage.setBackground(Color.BLUE);
		this.add(this.lblTopImage, BorderLayout.NORTH);

		GridBagConstraints c = new GridBagConstraints();
		Insets lblInsets = new Insets(5, 140, 5, 5);
		Insets txtInsets = new Insets(5, 5, 5, 30);

		JPanel centerPanel = UITools.createPanel(new GridBagLayout());
		this.add(centerPanel, BorderLayout.CENTER);

		c.fill = GridBagConstraints.BOTH;
		c.insets = lblInsets;
		UITools.createLabel("账户：", centerPanel, c);

		c.weightx = 1.0;
		c.insets = txtInsets;
		this.txtUserName = UITools.createSingleText("", centerPanel, c);

		c.weightx = 0.0;
		c.gridy = 1;
		c.insets = lblInsets;
		UITools.createLabel("密码：", centerPanel, c);

		c.weightx = 1.0;
		c.insets = txtInsets;
		this.txtPassword = UITools.createPasswordField("", centerPanel, c);

		GridBagConstraints b = new GridBagConstraints();
		JPanel buttomPanel = UITools.createPanel(new GridBagLayout());
		this.add(buttomPanel, BorderLayout.SOUTH);

		b.fill = GridBagConstraints.BOTH;
		b.insets = new Insets(5, 200, 15, 10);
		this.btnLogin = UITools.createButton("登录", buttomPanel, b);

		b.insets = new Insets(5, 10, 15, 30);
		this.btnCancel = UITools.createButton("取消", buttomPanel, b);
	}

	@Override
	protected void initListener() {
		// 登录按钮
		this.btnLogin.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				login();
			}
		});

		// 取消按钮
		this.btnCancel.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				
				MainWin win = new MainWin();
				win.open();
				close();
			}
		});

		// 账户键入事件
		this.txtUserName.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				super.keyPressed(e);
				enterKeyEvent(e);
			}
		});

		// 密码输入事件
		this.txtPassword.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				super.keyPressed(e);
				enterKeyEvent(e);
			}
		});
	}

	@Override
	protected void initStyle() {
		this.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
		// 禁止调整大小
		this.setResizable(false);
		// 设置居中
		this.setLocationRelativeTo(null);

		// 标题
		this.setTitle(DEFAULT_TITLE);
	}
	
	@Override
	protected void beforeOpen() {
 
	}

	/**
	 * 回车响应时间
	 * 
	 * @param e
	 */
	private void enterKeyEvent(KeyEvent e) {
		int keyCode = e.getKeyCode();
		if (keyCode == 10) {
			this.login();
		}
	}

	/**
	 * 登录处理
	 */
	private void login() {
		System.out.println("login");
		String userName = this.txtUserName.getText().trim();
		String password = this.txtPassword.getPassword().toString();

		// UITools.showErrorMessageBox("账户或密码不能为空！",this);
		PosWin posWin = new PosWin();
		posWin.open();

		super.close();
	}

}
